package com.iss.okair.service.assets;

import com.iss.common.StringTools;
import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsPermanent;
import com.iss.okair.db.entity.Category;
import com.iss.okair.db.mapper.AssetsPermanentMapper;
import com.iss.okair.db.mapper.CategoryMapper;
import com.iss.okair.db.model.AssetsPermanentModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

    @Service
    public class AssetsPermanentService {

        @Resource
        private AssetsPermanentMapper mapper;
        @Resource
        private CategoryMapper cmapper;

        public List<AssetsPermanent> find(AssetsPermanentModel model){
            return mapper.select(model);
        }

        //保存
        public boolean save(AssetsPermanent permanent){
            if(StringTools.isNullOrEmpty(permanent.getId())) {
                //添加
                //设置id值
                permanent.setId(UuidTools.buildUUID());
                //调用私有方法，生成编码
                permanent.setAssetscode(this.buildAssetsCode(permanent.getAssetstype()));
                return mapper.add(permanent)>=1?true:false;
            }
            else{
                //修改

                return mapper.update(permanent)>=1?true:false;
            }
        }

        private String buildAssetsCode(int categoryId){
            //1.根据资产类别的id，查询出来资产类别的信息
            Category currentCategory = cmapper.selectById(categoryId);

            //2.根据查询结果和规则，生成编码
            String temp = String.format("PER%s-%06d",currentCategory.getCode(),mapper.countALL()+1);

            //3.将编码抛出
            return temp;
        }
    }

